﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CadDotNetTools
{
    public static class EditorTools
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ed"></param>
        /// <param name="ext"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public static void Zoom(this Editor ed, Extents3d ext)
        {
            if (ed == null)
                throw new ArgumentNullException("ed");
            using (ViewTableRecord view = ed.GetCurrentView())
            {
                Matrix3d worldToEye = Matrix3d.WorldToPlane(view.ViewDirection) *
                    Matrix3d.Displacement(Point3d.Origin - view.Target) *
                    Matrix3d.Rotation(view.ViewTwist, view.ViewDirection, view.Target);
                ext.TransformBy(worldToEye);
                view.Width = ext.MaxPoint.X - ext.MinPoint.X;
                view.Height = ext.MaxPoint.Y - ext.MinPoint.Y;
                view.CenterPoint = new Point2d(
                    (ext.MaxPoint.X + ext.MinPoint.X) / 2.0,
                    (ext.MaxPoint.Y + ext.MinPoint.Y) / 2.0);
                ed.SetCurrentView(view);
            }
        }
        /// <summary>
        /// 缩放
        /// </summary>
        /// <param name="ed"></param>
        public static void ZoomExtents(this Editor ed)
        {
            Database db = ed.Document.Database;
            db.UpdateExt(false);
            Extents3d ext = (short)Application.GetSystemVariable("cvport") == 1 ?
                new Extents3d(db.Pextmin, db.Pextmax) :
                new Extents3d(db.Extmin, db.Extmax);
            ed.Zoom(ext);
        }
        /// <summary>
        /// 选择一个实体
        /// </summary>
        /// <param name="ed">Editor</param>
        /// <param name="message">提示信息</param>
        /// <param name="type">类型</param>
        /// <returns></returns>
        public static ObjectId GetEntity_(this Editor ed, string message, Type type = null)
        {
            if (ed is null) return ObjectId.Null;
            var opotion = new PromptEntityOptions(message);
            if (type != null)
            {
                opotion.SetRejectMessage(message);
                opotion.AddAllowedClass(type, true);
            }
            return ed.GetEntity(opotion).ObjectId;
        }
        /// <summary>
        /// GetSelection
        /// </summary>
        /// <param name="ed">editor</param>
        /// <param name="message">提示信息</param>
        /// <returns></returns>
        public static List<ObjectId> GetSelection_(this Editor ed, string message)
        {
            if (ed is null) return new List<ObjectId>();
            var opotion = new PromptSelectionOptions();
            opotion.MessageForAdding = message;
            opotion.RejectObjectsOnLockedLayers = false;

            var value = ed.GetSelection(opotion).Value;
            if (value is null) return new List<ObjectId>();
            return value.GetObjectIds().ToList();
        }
        /// <summary>
        /// 选择一个点
        /// </summary>
        /// <returns></returns>
        public static Point3d GetPoint()
        {
            var ed = Application.DocumentManager.MdiActiveDocument.Editor;
            //定义一个用户交互类
            var opt = new PromptPointOptions("\n 选择一个点酷酷酷");
            //允许None
            opt.AllowNone = true;
            var ppr = ed.GetPoint(opt);

            Point3d point = new Point3d();
            if (ppr.Status == PromptStatus.None) return point;
            if (ppr.Status == PromptStatus.Cancel) return point;
            if (ppr.Status == PromptStatus.OK)
            {
                point = ppr.Value;
            }
            return point;
        }
        /// <summary>
        /// 选择一个点
        /// </summary>
        /// <returns></returns>
        public static Point3d GetPoint(this Editor ed, string prompt)
        {
            //定义一个用户交互类
            var opt = new PromptPointOptions(prompt);
            //允许None
            opt.AllowNone = true;
            var ppr = ed.GetPoint(opt);

            Point3d point = new Point3d();
            if (ppr.Status == PromptStatus.None) return point;
            if (ppr.Status == PromptStatus.Cancel) return point;
            if (ppr.Status == PromptStatus.OK)
            {
                point = ppr.Value;
            }
            return point;
        }
    }
}
